<?php

/**
*文件夹下的图片处理成透明红色图片
*/
function getStamp($path,$path_new)
{
    $image = file_get_contents($path);
    $info = getimagesize($path);
    $im = imagecreatefromstring($image);
    $width = $info[0];
    $height = $info[1];
    for ($i = 0; $i < $height; $i += 1) {
        for ($j = 0; $j < $width; $j += 1) {
            $rgb = ImageColorAt($im, $j, $i);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            //将要变成红色的颜色rbg放入 如 $r> 100 && $r < 200 && $g > 100 && $g < 200 && $b > 100 && $b < 200 
            if ($r > 160 && $g > 160 && $b > 160) {
                $hex = imagecolorallocate($im, 255, 0, 0);
                imagesetpixel($im, $j, $i, $hex);
            } else {
                $hex = imagecolorallocate($im, 255, 255, 255);
                imagesetpixel($im, $j, $i, $hex);
            }
        }
    }
    $white = imagecolorallocate($im, 255, 255, 255);//拾取白色
    imagefill($im, 0, 0, $white);//把画布染成白色
    imagecolortransparent($im, $white);//把图片中白色设置为透明色
    imagepng($im, $path_new);
    return $path_new;
}

//扫描文件夹下的搜所有图片
$dir_files = "../../../../beitie_re_watermark/1/3/"; 
$dir_file_news = "../../../../beitie_re_watermark_alph/1/3/";
if(!is_dir($dir_file_news)) {
    mkdir($dir_file_news, 0777, true);
}
$dir_file_list = scandir($dir_files);
//$dir_file_list = glob("../../../../beitie_re/1/3/*.jpg");
//var_dump($dir_file_list);
foreach ($dir_file_list as $key => $value) {
	$suffix = substr($value, -3);
	if ($suffix == 'jpg') {
		$path = $dir_files . "/" . $value;
		$path_new = $dir_file_news . "/" . $value;
        getStamp($path, $path_new);
	}
}

